layui.config({
    base: 'layui/js' //你的模块目录
}).use(['form','jquery','layer','laypage'],function(){
    $ = layui.jquery;
    form = layui.form();
    function loadData(roleName,pageNumber){
        var url ='role/roleData?1=1';
        if(roleName!=''){
            url+='&roleName='+roleName;
        }
        if(pageNumber!=''){
            url+='&pageNumber='+pageNumber;
        }
        // 让加载的div显示出来动画
        var index = layui.layer.load(1);
        $.getJSON(url,function(ret){
        //ret pageInfo类型的Json对象
        var dataList = ret.list;
        var strHtml='';
        for(var i=0;i<dataList.length;i++){
            strHtml+='<tr>';
            strHtml+='<td><input type="checkbox" name="ids" value="'+dataList[i].id+'" lay-skin="primary" lay-filter="choose"/> </td>';
            strHtml+='<td>'+dataList[i].roleName+'</td>';
            strHtml+='<td>'+dataList[i].status+'</td>';
            strHtml+='<td><a class="layui-btn layui-btn-mini role_edit" data-id="'+dataList[i].id+'">编辑</a><a class="layui-btn layui-btn-danger layui-btn-mini  roles_del" data-id="'+dataList[i].id+'">删除</a><a class="layui-btn layui-btn-danger layui-btn-mini  permission_edit" data-id="'+dataList[i].id+'">权限管理 </a></td>';
            strHtml+='</tr>';
        }

        $(".role_content").html(strHtml);
            form.render();
            // 权限管理
            $(".permission_edit").click(function(){
                var id=$(this).attr("data-id");
              index = layui.layer.open({
                      type:2,
                      content:'resources/toPermission?id='+id,
                      area:["500px","400px"],
                      title:'角色权限',
                      area:["583px","430px"],
                      maxmin:true,
                      resize:true
                  });
                })
            $(".roles_del").click(function () {
                let id = $(this).attr("data-id");
                layui.layer.confirm("确定删除么？",function () {
                    $.get("role/delete?ids="+id,function (ret) {
                            if (ret){
                                layer.msg("删除成功！",function () {
                                    loadData("","");
                                });

                            }else {
                                layer.msg("删除失败！")
                            }
                        }

                    );
                })

            })

        $('.role_edit').click(function () {
            var id = $(this).attr("data-id");
                index = layui.layer.open({
                    type:2,
                    content:'role/add?id='+id,
                    title:'新增角色',
                    area:["583px","430px"],
                    maxmin:true,
                    resize:true
                });
        })
        //隐藏div
            layui.layer.close(index);
        // 分页
            $("#pageNumber").val(ret.pageNum);
        layui.laypage({
            cont:'page',
            pages:ret.pages,
            curr:ret.pageNum,
            groups:3,
            jump:function(obj,first){
                if(!first){
                    // 重新发请求

                    loadData($("#roleName").val(),obj.curr);
                }
            }
        });
    });
    }
    window.loadData=function(roleName,pageNumber){
        loadData(roleName,pageNumber);
    }
    loadData('','');
    $(".search_btn").click(function () {
        var roleName=$("#roleName").val();
        loadData(roleName,'');
    });

    $(".roleAdd_btn").click(function(){
        // 同步请求
        // form表单提交到指定的请求
        // document.location.href
        //
        index = layui.layer.open({
            type:2,
            content:'role/add',
            title:'新增角色',
            area:["583px","430px"],
            maxmin:true,
            resize:true
        });
    })

    $(".batchDel").click(function (){
        layui.layer.confirm("确定删除么？",function () {
            let data = $("#form1").serialize();

            $.get("role/delete?"+data,function (ret) {
                    if (ret){
                        layer.msg("删除成功！",function () {
                            loadData("","");
                        });

                    }else {
                        layer.msg("删除失败！")
                    }
                }

            );
        })
    })

}); //加载入口